package com.itheima.service;

import cn.hutool.core.collection.CollUtil;
import com.itheima.api.UmsResourceApi;
import com.itheima.api.UmsResourceCategoryApi;
import com.itheima.api.UmsRoleResourceRelationApi;
import com.itheima.pojo.UmsResource;
import com.itheima.pojo.UmsResourceCategory;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 袁鹏
 * @date 2022-10-13-12:28
 */
@Service
public class UmsResourceCategoryService {

    @DubboReference
    private UmsResourceCategoryApi umsResourceCategoryApi;

    @DubboReference
    private UmsResourceApi umsResourceApi;

    @DubboReference
    private UmsRoleResourceRelationApi umsRoleResourceRelationApi;


    /**
     * 添加后台资源分类
     * @param resourceCategory
     */
    public void create(UmsResourceCategory resourceCategory) {
        umsResourceCategoryApi.create(resourceCategory);
    }


    /**
     * 根据id删除后台资源分类
     */
    public void delete(Long id) {
        umsResourceCategoryApi.delete(id);
        // 删除分类下的资源
        // 根据分类id获取资源列表
        List<UmsResource> resourceList = umsResourceApi.getByCategoryId(id);
        // 提取资源id
        List<Long> idList = CollUtil.getFieldValues(resourceList, "id", Long.class);
        // 根据资源id删除资源
        umsResourceApi.delete(idList);
        // 删除资源与角色的关联
        umsRoleResourceRelationApi.deleteByResourceId(idList);
    }


    /**
     * 查询所有后台资源分类
     */
    public List<UmsResourceCategory> listAll() {
        return umsResourceCategoryApi.list();
    }


    /**
     * 修改后台资源分类
     */
    public void update(Long id, UmsResourceCategory resourceCategory) {
        resourceCategory.setId(id);
        umsResourceCategoryApi.update(resourceCategory);
    }
}
